package com.yic.module.mes.service.reportworkhours;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.mes.controller.admin.reportworkhours.vo.*;
import com.yic.module.mes.dal.dataobject.reportworkhours.ReportWorkHoursDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.mes.convert.reportworkhours.ReportWorkHoursConvert;
import com.yic.module.mes.dal.mysql.reportworkhours.ReportWorkHoursMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.mes.enums.ErrorCodeConstants.*;

/**
 * 报工工时 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ReportWorkHoursServiceImpl implements ReportWorkHoursService {

    @Resource
    private ReportWorkHoursMapper reportWorkHoursMapper;

    @Override
    public Long createReportWorkHours(ReportWorkHoursCreateReqVO createReqVO) {
        // 插入
        ReportWorkHoursDO reportWorkHours = ReportWorkHoursConvert.INSTANCE.convert(createReqVO);
        reportWorkHoursMapper.insert(reportWorkHours);
        // 返回
        return reportWorkHours.getId();
    }

    @Override
    public void batchCreateReportWorkHours(List<ReportWorkHoursCreateReqVO> createReqVOS) {
        List<ReportWorkHoursDO> reportWorkHourses = ReportWorkHoursConvert.INSTANCE.convertList03(createReqVOS);
        reportWorkHoursMapper.insertBatch(reportWorkHourses);
    }

    @Override
    public void updateReportWorkHours(ReportWorkHoursUpdateReqVO updateReqVO) {
        // 校验存在
        validateReportWorkHoursExists(updateReqVO.getId());
        // 更新
        ReportWorkHoursDO updateObj = ReportWorkHoursConvert.INSTANCE.convert(updateReqVO);
        reportWorkHoursMapper.updateById(updateObj);
    }

    @Override
    public void deleteReportWorkHours(Long id) {
        // 校验存在
        validateReportWorkHoursExists(id);
        // 删除
        reportWorkHoursMapper.deleteById(id);
    }

    private void validateReportWorkHoursExists(Long id) {
        if (reportWorkHoursMapper.selectById(id) == null) {
            throw exception(REPORT_WORK_HOURS_NOT_EXISTS);
        }
    }

    @Override
    public ReportWorkHoursDO getReportWorkHours(Long id) {
        return reportWorkHoursMapper.selectById(id);
    }

    @Override
    public List<ReportWorkHoursDO> getReportWorkHoursList(Collection<Long> ids) {
        return reportWorkHoursMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ReportWorkHoursDO> getReportWorkHoursPage(ReportWorkHoursPageReqVO pageReqVO) {
        return reportWorkHoursMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ReportWorkHoursDO> getReportWorkHoursList(ReportWorkHoursExportReqVO exportReqVO) {
        return reportWorkHoursMapper.selectList(exportReqVO);
    }

    @Override
    public void deleteReportWorkHoursByReportId(Long id) {
        reportWorkHoursMapper.deleteByReportId(id);
    }

    @Override
    public List<ReportWorkHoursFormsRespVO> getReportWorkHoursForms(ReportWorkHoursFormsReqVO reqVO) {
        return reportWorkHoursMapper.selectForms(reqVO);
    }

}
